//
//  XYTabBar.swift
//  WeiBo
//
//  Created by 陈修宇 on 16/3/4.
//  Copyright © 2016年 陈修宇. All rights reserved.
//

import UIKit

class XYTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
    
         setupUI()
    
    }
    // 设置按钮的视图
    
    private func setupUI(){
        
        // 添加子控件
        addSubview(composeButton)
        
        
    }
    required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        // 调整子控件的大小
        
        let width = self.frame.width/5
        
        var index = 0
        
          // 1.取到对应按钮
        for value in self.subviews{
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                // 2.计算对应按钮 x
                
                let x = CGFloat(index) * width
                
                
                // 3.设置大小
                
                value.frame.size.width = width
                
                value.frame.origin.x = x
                
                
                index++
                // 如果下一个按钮是第三个,那就往后移一个位置
                if index == 2{
                    
                    index++
                    
                }

            
            }
        
        
        }
        
        
        // 调整自定义button的位置
        
        composeButton.center = CGPoint(x: self.center.x, y: self.frame.height*0.5)
        
    }
    
    
    
    

    // MARK: - 懒加载控件
    
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        
        // 设置背景图
        // 高亮
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        // 默认
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        // 设置图片
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    
        // 设置按钮大小
        // 与图片一样大小
        button.sizeToFit()
    
    
        return button
    }()


    

}







